import { createSlice } from '@reduxjs/toolkit'

export const parentCard = createSlice({
    name: 'parentCard',
    initialState: [
        { id: 1, status: false, name: '赛罗', tel: '13245434549', code: '38674950049384059A' },
        { id: 2, status: true, name: '迪迦', tel: '1008611', code: '38674950049384059B' },
        { id: 3, status: false, name: '佐菲', tel: '12306', code: '38674950049384059C' },
        { id: 4, status: true, name: '捷德', tel: '14434334334', code: '38674950049384059D' },
    ],
    reducers: {
        changeParent(state, { payload }) {
            state[payload.ind].status = payload.checked
        },
        addParent(state, { payload }) {
            state.push({ ...payload, id: Math.random(), status: false })
        },
        delParent(state,{payload}){
            state.splice(payload,1)
        },
        editParent(state,{payload}){
            state.forEach(item=>{
                if(item.id==payload.id){
                    item.name=payload.name
                    item.tel=payload.tel
                    item.code=payload.code
                }
            })
        },
        setUsrt(state,actions){
            return {...state,...actions.payload}
        }
    },
})


export const { changeParent,addParent,delParent,editParent,setUsrt } = parentCard.actions

export default parentCard.reducer